package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_15_12S extends SkymobiProtocolMessage {
	int hallState;
	List<MmochatGang> gangList = null;
	int curPage;

	public Mmochat_15_12S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_15_12);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (gangList == null) {
			return;
		}
		out.putUnsignedShort(hallState);
		int gangNum = gangList.size();
		int pageNum = (int) Math.ceil(1.0 * gangNum
				/ MmochatConstant.maxGangNumShowPerPage);
		curPage = Math.min(curPage, pageNum - 1);
		curPage = Math.max(curPage, 0);		
		int showGangNum = Math.min(gangNum - curPage
				* MmochatConstant.maxGangNumShowPerPage,
				MmochatConstant.maxGangNumShowPerPage);

		out.putInt(pageNum);
		out.putInt(curPage);
		out.put((byte) showGangNum);
		if (showGangNum > 0) {
			Collections.sort(gangList, Collections.reverseOrder());
			int baseIndex = curPage * MmochatConstant.maxGangNumShowPerPage;
			for (int i = 0; i < showGangNum; i++) {
				MmochatGang gang = gangList.get(i + baseIndex);
				out.putInt(gang.getId());
				out.putPrefixedString(gang.getName(), 1);
				out.putPrefixedString(gang.getLevel().toString() + "帮派", 1);
				out.putInt(gang.getMembers().size());
				out.putInt(gang.getLevel().getMaxMemberNum());
			}
		}
	}

	public void setHallState(int hallState) {
		this.hallState = hallState;
	}

	public void setGangList(List<MmochatGang> gangList) {
		this.gangList = new ArrayList<MmochatGang>(gangList);
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

}